package com.scvtc.controller.system;

import com.scvtc.domain.ResResult;
import com.scvtc.domain.dto.system.user.UserEnableDto;
import com.scvtc.domain.dto.system.user.UserInfoDto;
import com.scvtc.domain.dto.system.user.UserRoleDto;
import com.scvtc.domain.vo.PageVo;
import com.scvtc.domain.vo.system.user.UserListVo;
import com.scvtc.domain.vo.system.user.UserRoleIdsDto;
import com.scvtc.service.system.SystemUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Digits;
import java.util.List;

import static com.scvtc.constants.SystemCheckedConstants.PAGE_CURRENT_ERROR;
import static com.scvtc.constants.SystemCheckedConstants.PAGE_SIZE_ERROR;
import static com.scvtc.constants.SystemConstants.PAGE_CURRENT;
import static com.scvtc.constants.SystemConstants.PAGE_SIZE;

/**
 * 用户管理(SystemUser)表控制层
 *
 * @author yiFei
 * @since 2022-04-13 17:38:28
 */
@Api(tags = "用户管理(SystemUser)")
@RestController
@RequestMapping("systemUser")
public class SystemUserController {
    /**
     * 用户管理(SystemUser)对象
     */
    @Autowired
    private SystemUserService systemUserService;

    @GetMapping("/list")
    @ApiOperation("查询所有用户")
    @PreAuthorize("hasAuthority('system:user:list')")
    public ResResult<PageVo<List<UserListVo>>> getAllUser(
            @Digits(integer = 5, fraction = 1, message = PAGE_CURRENT_ERROR) @RequestParam(defaultValue = PAGE_CURRENT) Long current,
            @Digits(integer = 3, fraction = 0, message = PAGE_SIZE_ERROR) @RequestParam(defaultValue = PAGE_SIZE) Long size,
            UserInfoDto userInfo
    ) {
        return systemUserService.getAllUser(current, size, userInfo);
    }

    @PostMapping("/")
    @ApiOperation("增加用户")
    @PreAuthorize("hasAuthority('system:user:add')")
    public ResResult<?> addUser(@RequestBody UserRoleDto userRoleDto) {
        return systemUserService.addUser(userRoleDto);
    }

    @PutMapping("/")
    @ApiOperation("修改用户信息")
    @PreAuthorize("hasAuthority('system:user:update')")
    public ResResult<?> updateUser(@RequestBody UserInfoDto userInfoDto) {
        return systemUserService.updateUser(userInfoDto);
    }

    @PutMapping("/update/role")
    @ApiOperation("修改用户角色信息")
    @PreAuthorize("hasAuthority('system:user:update')")
    public ResResult<?> updateUserRoleIdsDto(@RequestBody UserRoleIdsDto userRoleIdsDto) {
        return systemUserService.updateUserRoleIdsDto(userRoleIdsDto.getUserId(), userRoleIdsDto.getRoleIds());
    }

    @DeleteMapping("/")
    @ApiOperation("删除用户")
    @PreAuthorize("hasAuthority('system:user:delete')")
    public ResResult<?> deleteUser(Long id) {
        return systemUserService.deleteUser(id);
    }

    @PutMapping("/password")
    @ApiOperation("初始化用户密码")
    @PreAuthorize("hasAuthority('system:user:update')")
    public ResResult<?> initUserPassword(Long id) {
        return systemUserService.initUserPassword(id);
    }


    @PutMapping("/enable")
    @ApiOperation("禁用账号")
    @PreAuthorize("hasAuthority('system:user:update')")
    public ResResult<?> enableUser(@RequestBody UserEnableDto userEnableDto) {
        return systemUserService.enableUser(userEnableDto.getId(), userEnableDto.getStatus());
    }

    @PutMapping("/offline")
    @ApiOperation("强制用户下线")
    @PreAuthorize("hasAuthority('system:user:update')")
    public ResResult<?> offline(Long id) {
        return systemUserService.offline(id);
    }
}

